package com.ruoyi.manufacturer.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.manufacturer.domain.DjManufacturer;
import com.ruoyi.manufacturer.service.DjManufacturerService;
import com.ruoyi.product.domain.ProductModel;
import com.ruoyi.product.service.ProductServer;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 生产商管理Controller
 *
 * @author zjh
 * @date 2022-04-06
 */
@Controller
@RequestMapping("/manufacturer")
public class DJManufacturerController extends BaseController {
    private String prefix = "manufacturer";

    @Autowired
    private DjManufacturerService djManufacturerService;

    @Autowired
    private ProductServer productServer;

    @RequiresPermissions("business:manufacturer:view")
    @GetMapping()
    public String manufacturer() {
        return prefix + "/manufacturer";
    }

    /**
     * 查询生产商管理列表
     */
    @RequiresPermissions("business:manufacturer:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(DjManufacturer djManufacturer) {
        startPage();
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        djManufacturer.setCompanyId(sysUser.getCompanyId());
        List<DjManufacturer> list = djManufacturerService.selectDjManufacturerList(djManufacturer);
        return getDataTable(list);
    }

    /**
     * 导出生产商管理列表
     */
    @RequiresPermissions("business:manufacturer:export")
    @Log(title = "生产商管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public void export(DjManufacturer djManufacturer, HttpServletResponse response) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        djManufacturer.setCompanyId(sysUser.getCompanyId());
        List<DjManufacturer> list = djManufacturerService.selectDjManufacturerList(djManufacturer);
        ExcelUtil<DjManufacturer> util = new ExcelUtil<DjManufacturer>(DjManufacturer.class);
        util.exportExcel(response, list, "生产管理商数据");
    }

    /**
     * 新增生产商管理
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存生产商管理
     */
    @RequiresPermissions("business:manufacturer:add")
    @Log(title = "生产商管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody DjManufacturer djManufacturer) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        djManufacturer.setCreatBy(sysUser.getUserName());
        djManufacturer.setCompanyId(sysUser.getCompanyId());
        return toAjax(djManufacturerService.insertDjManufacturer(djManufacturer));
    }

    /**
     * 修改生产商管理
     */
    @GetMapping("/edit/{manufacturerId}")
    public String edit(@PathVariable("manufacturerId") Long manufacturerId, ModelMap mmap) {

        DjManufacturer djManufacturer = djManufacturerService.selectDjManufacturerById(manufacturerId);
        mmap.put("djManufacturer", djManufacturer);
        return prefix + "/edit";
    }

    /**
     * 修改保存生产商管理
     */
    @RequiresPermissions("business:manufacturer:edit")
    @Log(title = "生产商管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody DjManufacturer djManufacturer) {

        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        djManufacturer.setUpdateBy(sysUser.getUserName());
        return toAjax(djManufacturerService.updateDjManufacturer(djManufacturer));
    }

    /**
     * 删除生产商管理
     */
    @RequiresPermissions("business:manufacturer:remove")
    @Log(title = "生产商管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(@RequestBody JSONObject manufacturerId) {

        Long a = manufacturerId.getLongValue("manufacturerId");
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long companyId = sysUser.getCompanyId();
        ProductModel productModel = new ProductModel();
        productModel.setDeptId(companyId);
        productModel.setManufacturerId(a);
       /* List<ProductModel> productModelList = productServer.selectProductModel(productModel);
        if (productModelList.size() != 0) {
            return AjaxResult.error("该供应商正在使用，请先清理产品!");
        }*/
        return toAjax(djManufacturerService.deleteDjManufacturerById(a));
    }

}
